﻿using System;
using System.Drawing;
using System.Reflection;
using System.Web.UI;
using VitCode.BaseDataPageCreator;
using VitCode.Castle.DataAccess;
using VitCode.Web.UI.WebControls;

namespace AMSInventoryWeb.UserControls
{
    public partial class ChildObjectSetCtl : UserControl
    {
        private BaseDataAccess m_Acc = new BaseDataAccess();
        private PageLoadHelper m_LoadHelper;

        public Color BackColor
        {
            get { return txtObjVal.BackColor; }
            set { txtObjVal.BackColor = value; }
        }

        public bool Enabled
        {
            get { return txtObjVal.Enabled; }
            set
            {
                txtObjVal.Enabled = value;
                ibtSeach.Visible = value;
            }
        }

        public string InitCurrUser
        {
            get { return hdnInitCurrUser.Value; }
            set
            {
                hdnInitCurrUser.Value = value;
                if (hdnInitCurrUser.Value == "true")
                {
                    txtObjId.Text = GenericContext.CurrUserId.ToString();
                    txtObjVal.Text = GenericContext.CurrUserName;
                    hdnKeyProVal.Value = GenericContext.CurrUserId.ToString();
                    hdnShowProVal.Value = GenericContext.CurrUserName;
                }
            }
        }

        public string ObjClassName
        {
            get { return hdnClassName.Value; }
            set { hdnClassName.Value = value; }
        }

        public string ObjTypeName
        {
            get { return hdnTypeName.Value; }
            set { hdnTypeName.Value = value; }
        }

        public string ObjKeyProName
        {
            get { return hdnKeyProName.Value; }
            set { hdnKeyProName.Value = value; }
        }

        public string ObjShowProName
        {
            get { return hdnShowProName.Value; }
            set { hdnShowProName.Value = value; }
        }

        public string ConditionString
        {
            get { return hdnConditionString.Value; }
            set { hdnConditionString.Value = value; }
        }

        public object ObjKeyProVal
        {
            get { return txtObjId.Text; }
            set
            {
                if (value != null)
                {
                    txtObjId.Text = m_LoadHelper.GetProObjStringValue(ObjKeyProName, value);
                    hdnKeyProVal.Value = txtObjId.Text;
                }
            }
        }

        public object ObjShowProVal
        {
            get { return txtObjVal.Text; }
            set
            {
                if (value != null)
                {
                    txtObjVal.Text = m_LoadHelper.GetProObjStringValue(ObjShowProName, value);
                    hdnShowProVal.Value = txtObjVal.Text;
                }
            }
        }

        public string OtherCtlContext
        {
            get { return hdnOtherCtlContext.Value; }
            set { hdnOtherCtlContext.Value = value; }
        }

        private string BaseDataSettingXmlPath
        {
            get { return AppDomain.CurrentDomain.BaseDirectory + "Setting\\BaseDataSet.xml"; }
        }

        protected override void OnInit(EventArgs e)
        {
            if (m_LoadHelper == null)
                m_LoadHelper = new PageLoadHelper(BaseDataSettingXmlPath);
            base.OnInit(e);
        }

        private string GetConditionSqlString()
        {
            string[] ss = ConditionString.Split('=');
            if (ss != null && ss.Length == 2)
            {
                string strProName = ss[0];
                string strCtlValInfo = ss[1];
                string strSqlColName = m_LoadHelper.XmlReader.GetProXMLNodeAttrValue(ObjClassName, ObjTypeName,
                                                                                     strProName, m_LoadHelper.MEMO);
                if (strSqlColName.Length > 0)
                {
                    string[] iss = strCtlValInfo.Split('.');
                    if (iss != null && iss.Length == 2)
                    {
                        string ctlId = iss[0];
                        string ctlProName = iss[1];
                        object ctl = Parent.FindControl(ctlId);
                        if (ctl != null)
                        {
                            PropertyInfo ctlPi = ctl.GetType().GetProperty(ctlProName);
                            if (ctlPi != null)
                            {
                                object ctlPiVal = ctlPi.GetValue(ctl, null);
                                if (ctlPiVal != null)
                                    return "(" + strSqlColName + "='" + ctlPiVal + "')";
                            }
                        }
                    }
                }
            }
            return "";
        }

        private void ShowDialog()
        {
            string selectPageUrlValue = "/BaseData/SelObjList.aspx";
            var tempWin = new Window(Page, ObjClassName + "选择页面");
            tempWin.Width = 800;
            tempWin.Height = 600;
            tempWin.Scrollbars = true;
            tempWin.Status = true;
            var reValues = new[] {txtObjId.ClientID, txtObjVal.ClientID, hdnKeyProVal.ClientID, hdnShowProVal.ClientID};
            selectPageUrlValue += "?" + m_LoadHelper.CLASS_NAME + "=" + ObjClassName;
            if (ObjTypeName.Length > 0)
                selectPageUrlValue += "&" + m_LoadHelper.TYPE_NAME + "=" + ObjTypeName;
            if (ObjShowProName.Length > 0)
                selectPageUrlValue += "&" + m_LoadHelper.KEY_CONTEXT + "=" + ObjShowProName + ":1," + ObjKeyProName +
                                      ":2," + ObjShowProName + ":3";
            if (ObjKeyProName.Length > 0)
                selectPageUrlValue += "&" + m_LoadHelper.KEY_PROPERTY + "=" + ObjKeyProName + ":0";
            if (ConditionString.Length > 0)
            {
                string val = GetConditionSqlString();
                selectPageUrlValue += "&" + m_LoadHelper.CONDITION_STRING + "=" + Server.UrlEncode(val);
            }
            if (OtherCtlContext.Length > 0)
                selectPageUrlValue += "&" + m_LoadHelper.OTHER_CTL_CONTEXT + "=" + OtherCtlContext;
            tempWin.ShowModalDialog(selectPageUrlValue, reValues, false);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (txtObjId.Text.Length == 0 && hdnKeyProVal.Value.Length > 0)
                txtObjId.Text = hdnKeyProVal.Value;
            if (txtObjVal.Text.Length == 0 && hdnShowProVal.Value.Length > 0)
                txtObjVal.Text = hdnShowProVal.Value;
        }

        protected void ibtSeach_Click(object sender, ImageClickEventArgs e)
        {
            ShowDialog();
        }
    }
}